﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
  public static class EnumEx
  {
    public static void ForEach<T>(Action<T> action) where T : struct
    {
      #region Validation
      if (!typeof(T).IsEnum)
        throw new ArgumentException("T must be an enumeration");
      #endregion
      foreach (string name in Enum.GetNames(typeof(T)))
        action((T)Enum.Parse(typeof(T), name, true));
    }

    public static IEnumerable<T> GetEnumerator<T>()
    {
      #region Validation
      if (!typeof(T).IsEnum)
        throw new ArgumentException("T must be an enumeration");
      #endregion
      foreach (string name in Enum.GetNames(typeof(T)))
        yield return (T)Enum.Parse(typeof(T), name, true);
    }
    
    public static T Parse<T>(string memberName)
    {
      return (T)Enum.Parse(typeof(T), memberName, true);
    }
  }
}
